<?php

namespace Admin\Controller;
use Admin\Model\UserModel;
use Think\Controller;
use Admin\Model\AdminUserModel;
use Admin\Model\ArticleModel;
use Admin\Model\ItemModel;
use Admin\Model\ApplicationModel;
use Admin\Model\CommunicateModel;
use Admin\Model\CommentModel;
class TestController extends Controller
{
    public function testAdminUserList()
    {
        $user = new AdminUserModel();
        $res = $user->queryAllAdminUser();
        $adminUserList = $res['list'];
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$adminUserList));
    }

    //微信绑定手机号
    public function bindPhone ()
    {
        $phone = $_GET['phone'];
    }

    //小程序前台首页显示2篇文章,按发布时间倒序
    public function queryAllArticle ()
    {
        $art = new ArticleModel();
        $res = $art -> queryAllArticle();
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }

    //小程序前台显示所有文章,按发布时间倒序
    public function queryAllArticles ()
    {
        $art = new ArticleModel();
        $res = $art -> queryAllArticles();
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }

    //党员类型查询
    public function queryAllType ()
    {
        $type = M("type");
        $res = $type->select();
        foreach($res as $val){
            $data[] = $val['name'];
        }
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$data));
    }

    //前台查询所有捐款项目
    public function queryItem()
    {
        $item = new ItemModel();
        $res = $item -> queryItem();
        foreach ($res as $key=>$val){
            $a = (string)($val['end_time']);
            $temp = substr($a,0,7);
            $res[$key]['end_time'] = $temp;
        }
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }

    //前台查询所有捐款项目 只要捐款 项目
    public function queryAllItem ()
    {
        $item = new ItemModel();
        $res = $item -> queryItem();
        foreach ($res as $key=>$val){
            $data[] = $val['item'];
        }
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$data));
    }

    //提交组织关系转出申请时,查看个人信息
    public function queryUserByOpenId()
    {
        $OpenId = $_GET['id'];
        $user = new UserModel();
        $userInfo = $user -> queryUserByOpenId($OpenId);
        $userInfo['fee_time'] = date("Y年m月d日",strtotime($userInfo['fee_time']));
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$userInfo));
    }

    //把组织关系转出申请信息,保存到数据库
    public function saveApplication ()
    {
        $data['department'] = $_GET['department'];
        $data['user_id'] = $_GET['user_id'];
        $data['applica_time'] = date("Y-m-d H:i:s");
        $data['applica_status'] = 0;
        $app = new ApplicationModel();
        $res = $app -> saveApplication($data);
        echo $res;
    }

    //查看所有党员
    public function queryAllUser ()
    {
        $user = new UserModel();
        $res = $user -> queryAllUserByPhone();
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }

    //查看所有小组
    public function queryAllGroup()
    {
        $group = M("group");
        $res = $group -> select();
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }

    //查询党员学习交流文章 2
    public function queryCommLimit ()
    {
        $com = new CommunicateModel();
        $res = $com -> queryCommLimit();
        //查询每篇文章的评论数
        foreach($res as $key=>$val){
            //获取文章id
            $id = $val['id'];
            //根据文章id查询评论总数
            $comm = D("comment");
            $result = $comm -> queryTotalComment($id);
            $res[$key]['com'] = $result['total'];//评论总数
            $res[$key]['num'] = $key+1;//文章序号
        }
        echo json_encode(array("status"=>"success","code"=>200,"data"=>$res));
    }
}